package bishi.duoduo;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @Classname Main2
 * @Description TODO
 * @Date 2021/9/22 15:51
 * @Created by ASUS
 */
public class Main2 {
    /*
3
3 3 7
1 1 2
5 5 8
3 9 7
3 3 6
10 9 6
10 10 10
1 7 4
3 3 7
1 4 9
10 6 10
3 3 5

    * */
    static int[][] amount;
    static boolean[][] visited;
    static Set<Integer> set = new HashSet<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < num; i++) {
            cnt = 0;
            set.clear();
            String[] s = scanner.nextLine().split(" ");
            int m = Integer.parseInt(s[0]), n = Integer.parseInt(s[1]), k = Integer.parseInt(s[2]);
            int[][] mat = new int[m][n];
            amount = new int[m][n];
            visited = new boolean[m][n];
            for (int j = 0; j < m; j++) {
                s = scanner.nextLine().split(" ");
                for (int r = 0; r < n; r++) {
                    mat[j][r] = Integer.parseInt(s[r]);
                    amount[j][r] = -1;
                }
            }
            dfs(mat, 0, 0, visited, k);
            System.out.println(cnt);
        }
    }

    static int[][] dirs = new int[][]{{1, 0}, {0, 1}};
    static int cnt = 0;

    static void dfs(int[][] mat, int x, int y, boolean[][] visited, int k) {
        if (k == 0) return;
        visited[x][y] = true;
        if (x == mat.length - 1 && y == mat[0].length - 1) {
            cnt++;
            return;
        }
        for (int[] dir : dirs) {
            int dx = x + dir[0], dy = y + dir[1];
            if (dx < 0 || dx == mat.length || dy < 0 || dy == mat[0].length) continue;
            if (!visited[dx][dy] && !set.contains(mat[dx][dy])) {
                set.add(mat[dx][dy]);
                dfs(mat, dx, dy, visited, k - 1);
                set.remove(mat[dx][dy]);
            }
        }
        visited[x][y] = false;
    }
}
